package com.yonyou.http;

import com.yonyou.cloud.auth.sdk.client.AuthSDKClient;
import com.yonyou.cloud.auth.sdk.client.utils.http.HttpResult;
import com.yonyou.iuap.context.InvocationInfoProxy;
import com.yonyou.ypd.bill.basic.spring.YpdSpringContextHolder;
import com.yonyoucloud.iuap.ucf.mdd.starter.ucg.openapi.isv.router.module.isvrequest.ISVRequest;
import com.yonyoucloud.iuap.ucf.mdd.starter.ucg.openapi.isv.router.module.isvrequest.ISVRequestBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: zhuyjh
 * @Date: 2024/6/1 11:31
 */
@Slf4j
public class IsvAuthSDKClient extends AuthSDKClient {
    private static ISVRequest isvRequest = YpdSpringContextHolder.getContext().getBean(ISVRequest.class);
    public IsvAuthSDKClient(String accessKey, String accessSecret) {
        super(accessKey, accessSecret);
    }
    /**
     * 执行请求
     *
     * @param request Http 请求对象(org.apache.http.client.methods.HttpGet, HttpPost, HttpDelete, HttpPut, etc.)
     * @return 请求结果
     */
    public HttpResult execute(HttpRequestBase request) {
        if (InvocationInfoProxy.getYhtAccessToken()!=null && !InvocationInfoProxy.getYhtAccessToken().startsWith("btt")) {
            ISVRequestBody isvRequestBody = new ISVRequestBody();
            String url = request.getURI().toString();
            isvRequestBody.setUrl(url);
            //判断类型
            String method = request.getMethod();
            if(method.equals("POST")){
                method = "POSTJSON";
            }
            isvRequestBody.setRequestMethod(method);
            HttpEntity httpEntity = ((HttpEntityEnclosingRequestBase)request).getEntity();
            if(httpEntity instanceof StringEntity) {
                HashMap<String, String> hashMap = new HashMap<>();
                String jsonString = null;
                try {
                    jsonString = new BufferedReader(new InputStreamReader(httpEntity.getContent(), StandardCharsets.UTF_8))
                            .lines()
                            .collect(Collectors.joining(System.lineSeparator()));
                } catch (IOException e) {
                    log.error("httpEntity.getContent转string失败"+ e.getMessage(),e);
                }
                hashMap.put("data", jsonString);
                isvRequestBody.setParams(hashMap);
            }
            String yhtAccessToken = InvocationInfoProxy.getYhtAccessToken();
            Map<String, String> headerMap = new HashMap<>();
            headerMap.put("yht_access_token", yhtAccessToken);
            headerMap.put("Content-Type", "application/json");
            isvRequestBody.setHeaders(headerMap);
            String result = isvRequest.doRequest(InvocationInfoProxy.getTenantid(), isvRequestBody);
            HttpResult httpResult = new HttpResult();
            httpResult.setResponseString(result);
            httpResult.setStatusCode(200);
            return httpResult;
        }
        return super.execute(request);

    }
}